package com.hunk.core.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 此类表示一个sheet的配置信息，及sheet的验证处理等操作。
 * @author HunkDeng
 */
public class PoiSheet {
	/** 对应excel模版的名字 **/
	protected String excelTemplateName;
	/** 此sheet的版本号 **/
	protected String version;
	/** 临时表名 **/
	protected String tempTableName;
	
	/** 需要解析的sheet名字 **/
	protected String sheetName;
	/** 数据开始的行数 **/
	protected int dataBeginRow;
	/** 一个sheet里面包含了多个字段 **/
	protected List<PoiField> poiFields = new ArrayList<PoiField>();
	/** 每次解析后，一批数据拥有一个uuid **/
	protected PoiField importUUID;
	/** 每一个数据都有一个主键id **/
	protected PoiField dataId;
	/** 每一个数据都有一个主键id **/
	protected PoiField errorMessage;
	
	/** 根据配置信息,获得并校验需要解析的excel sheet
	 * @param hssfWorkbook
	 * 					需要解析的workbook
	 * @return
	 * 		  HSSFSheet
	 * 					需要解析的工作表sheet
	 * @throws VersionInCorrectException
	 */
	public HSSFSheet getParseSheet(HSSFWorkbook hssfWorkbook) throws VersionInCorrectException {
		HSSFSheet hssfSheet = hssfWorkbook.getSheet(sheetName);
		if (hssfSheet == null) {
             throw new VersionInCorrectException();
        }
		String _versionCellValue = ExcelUtils.getCellStrValue(hssfSheet, 0, 0, false);
		if (!version.equals(_versionCellValue)) {
        	throw new VersionInCorrectException();
        }
		return hssfSheet; 
	}

	/** 解析excel中每行的数据，存入一个map中
	 * @param hssfWorkbook
	 * @param hssfSheet
	 * @param hssfRow
	 * @param import_uuid 
	 * @return
	 */
	public final Map<String, String> parse(HSSFWorkbook hssfWorkbook, HSSFSheet hssfSheet,
			HSSFRow hssfRow, String import_uuid_str) {
		Map<String,String> entityMap = new HashMap<String, String>();
		entityMap.put("errorMessage", "");
		StringBuilder _rowValue = new StringBuilder();
		for (PoiField pf : poiFields) {
			HSSFCell _hssfCell = hssfRow.getCell(pf.colNumberInExcel);
			String _stringValue = ExcelUtils.getCellStrValue(_hssfCell, pf.isDateType());
			// 建立chain
			ProcessorChain _chain = new ProcessorChain(pf.getProcessors());
			_stringValue = _chain.process(hssfWorkbook, hssfSheet, hssfRow, _hssfCell, entityMap, _stringValue, _chain);
			
			entityMap.put(pf.fieldNameInClass, _stringValue);
			_rowValue.append(_stringValue);
		}
		// 判断这行是否有数据
		if ("".equals(_rowValue.toString().trim())) { 
			return null;	
		} else {
			entityMap.put(importUUID.fieldNameInClass, import_uuid_str);
			entityMap.put(dataId.fieldNameInClass, getData_id().toString());
			return entityMap;
		}
	}
	
	/**根据entity map保存到临时表中
	 * @param paresedData
	 * @return
	 */
	public List<String> insertToTempTable(List<Map<String, String>> paresedData) {
		List<String> sqlList = new ArrayList<String>();
		List<PoiField> _poiFields = new ArrayList<PoiField>(poiFields);
		_poiFields.add(importUUID);
		_poiFields.add(dataId);
		_poiFields.add(errorMessage);
		
		for (Map<String,String> entity : paresedData) {
			StringBuilder _insertSql = new StringBuilder();
			_insertSql.append("insert into " + tempTableName + "(");
			StringBuilder _insertSql2 = new StringBuilder();
			_insertSql2.append("values (");			
			
			for (PoiField pf : _poiFields) {
				if (pf.needSave && !"".equals(entity.get(pf.fieldNameInClass))) {
					_insertSql.append(pf.fieldNameInClass).append(",");
					_insertSql2.append("'").append(entity.get(pf.fieldNameInClass)).append("',");
				}
			}
			_insertSql.deleteCharAt(_insertSql.length() - 1);
			_insertSql.append(") ");
			_insertSql2.deleteCharAt(_insertSql2.length() - 1);
			_insertSql2.append(");");
			_insertSql.append(_insertSql2);
			sqlList.add(_insertSql.toString());
		}
		return sqlList;
	}

	public void setExcelTemplateName(String excelTemplateName) {
		this.excelTemplateName = excelTemplateName;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public void setTempTableName(String tempTableName) {
		this.tempTableName = tempTableName;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public void setDataBeginRow(int dataBeginRow) {
		this.dataBeginRow = dataBeginRow;
	}

	public void setPoiFields(List<PoiField> poiFields) {
		this.poiFields = poiFields;
	}
	
	public void setImportUUID(PoiField importUUID) {
		this.importUUID = importUUID;
	}

	public void setDataId(PoiField dataId) {
		this.dataId = dataId;
	}

	public Object getImport_uuid() {
		return null;
	}
	
	public Object getData_id() {
		return null;
	}

	public void setErrorMessage(PoiField errorMessage) {
		this.errorMessage = errorMessage;
	}
}
